//
//  LoadingLayer.cpp
//  PizzaMaker
//
//  Created by luotianqiang1 on 3/17/15.
//
//

#include "LoadingLayer.h"
#include "HomeLayer.h"
#include "LQ_adLoadingLayer.h"
#include "MyDialog.h"
#include "AdsManager.h"
#include "FoodData.h"
REGIST_SECENE(LoadingLayer)
LoadingLayer::~LoadingLayer(){
 
}

bool LoadingLayer::init() {
    if(BaseStepLayer::init()) {
        
        _operate = StudioLayer::create("LoadingLayer.csb");
        this->addChild(_operate);
        auto optionlayer = _operate->getNodeByName("optionlayer");
        for(auto child:optionlayer->getChildren())
            child->setVisible(false);
        vector<string> foodNames = {"food0","food3","food1","food2"};
        vector<ActionHelper::ShowDirection> dircs = {
             ActionHelper::ShowDirection::show_from_top
            ,ActionHelper::ShowDirection::show_from_top
            ,ActionHelper::ShowDirection::show_from_right
            ,ActionHelper::ShowDirection::show_from_left};
        for(int i=0;i<4;i++){
            auto food = _operate->getNodeByName(foodNames[i]);
            ActionHelper::delayFunc(0.2f*i, food, [=](){
                if(i%2==0)
                  SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
                ActionHelper::showBackInOut(food, food->getPosition(), dircs[i],[=](){
                    food->runAction(ActionHelper::getScaleBtnAction());
                    if(i==3){
                        SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
                        auto logo = _operate->getNodeByName("logo");
                        auto pos = logo->getPosition();
                        logo->setPositionY(0);
                        logo->setScale(0);
                        logo->runAction(ScaleTo::create(1, 1));
                        logo->runAction(Sequence::create(JumpTo::create(1, pos, 200, 1), CallFunc::create([logo,this](){
                            logo->runAction(ActionHelper::getScaleBtnAction());
                            auto loading = _operate->getNodeByName("loading");
                            loading->setOpacity(0);
                            loading->setVisible(true);
                            loading->runAction(Sequence::create(DelayTime::create(0.2f), FadeIn::create(0.6),CallFunc::create([this](){
                                for(int i=0;i<3;i++)
                                    dots.push_back(_operate->getNodeByName<Sprite*>(StringUtils::format("dian%d",i)));
                                Vector<FiniteTimeAction*> actions;
                                for(int i=0;i<dots.size();i++){
                                    actions.pushBack(CallFunc::create([=](){
                                        dots.at(i)->setVisible(true);
                                        dots.at(i)->setScale(0);
                                        dots.at(i)->runAction(ScaleTo::create(0.1, 1));}));
                                    actions.pushBack(DelayTime::create(0.2));
                                }
                                actions.pushBack(DelayTime::create(0.5));
                                actions.pushBack(CallFunc::create([this](){
                                    for(auto dot:dots)
                                        dot->runAction(ScaleTo::create(0, 0));
                                }));
                                actions.pushBack(DelayTime::create(1.2));
                                this->runAction(RepeatForever::create(Sequence::create(actions)));
                            }), nullptr));
                        }), nullptr));
                        logo->setVisible(true);
                    }
                },0.9f);
                food->setVisible(true);
            });
          
        }
       
         ActionHelper::delayFunc(6, this, CC_CALLBACK_0(LoadingLayer::enCallBack, this));
        return true;
        auto* contentLayer = Layer::create();
        contentLayer->ignoreAnchorPointForPosition(false);
        contentLayer->setContentSize(Size(960,640));
        contentLayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
        contentLayer->setPosition(getContentSize()*.5);
        this->addChild(contentLayer);
        
        auto bg = Sprite::create("loading/bg.jpg");
        bg->setPosition(contentLayer->getContentSize()*.5);
        contentLayer->addChild(bg);
        
        tents = Sprite::create("loading/z.png");
        tents->setAnchorPoint(Vec2(0.5,0.25));
        tents->setPosition(480.00,141.75);
        tents->setScale(1,0);
        contentLayer->addChild(tents);
        tents->runAction(Sequence::create(EaseBackOut::create(ScaleTo::create(0.5, 1)),CallFunc::create([this](){
            Vector<FiniteTimeAction*> actions;
            for(int i=0;i<dots.size();i++){
                actions.pushBack(CallFunc::create([=](){dots.at(i)->runAction(ScaleTo::create(0.1, 1));}));
                actions.pushBack(DelayTime::create(0.2));
            }
            actions.pushBack(DelayTime::create(0.5));
            actions.pushBack(CallFunc::create([this](){
                for(auto dot:dots)
                    dot->runAction(ScaleTo::create(0, 0));
            }));
            actions.pushBack(DelayTime::create(1.2));
            this->runAction(RepeatForever::create(Sequence::create(actions)));
        }),DelayTime::create(0.3),CallFunc::create([this](){
            float time = 0.25;
            tents->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(time*0.6, 1,1.03),ScaleTo::create(time, 1,0.96),Spawn::create(ScaleTo::create(time, 1),SkewTo::create(time,-7,0), nullptr),Spawn::create(Sequence::create(ScaleTo::create(time, 1,0.96),ScaleTo::create(time, 1), nullptr),SkewTo::create(time*2,7,0), nullptr),Spawn::create(ScaleTo::create(time, 1,0.96),SkewTo::create(time,0,0), nullptr),ScaleTo::create(time*0.6, 1),DelayTime::create(0.7), nullptr)));
        }), nullptr));
       
        
        float posX[] = {698.91,739.00,779.10};
        
        for(int i=0;i<3;i++){
            auto dot = Sprite::create("loading/d.png");
            dot->setPosition(posX[i],508.00);
            dot->setScale(0);
            dots.push_back(dot);
            contentLayer->addChild(dot);
        }
      
     
       
        return true;
    }
    return false;
}

void LoadingLayer::enCallBack(){
    
      Director::getInstance()->replaceScene(TransitionPageTurn::create(1.2,HomeLayer::createScene(),false));
    BaseStepLayer::insertBtnClick("btn_more",[this](ui::Widget*){
        _cfsys.showMoreGame();
    });
    BaseStepLayer::insertBtnClick("btn_home", [](ui::Widget*){
        auto _dialog = MyDialog::create("Do you want to start over with a new game?", MyDialog::twoBtnPath);
        _dialog->dialogBtnClick =[](MyDialog * dialog,int index){
            if(index == 0) {
                auto _endcallBack = [](){
                    Director::getInstance()->popToRootScene();
                    LQ_adLoadingLayer::loadingDoneCallback = nullptr;
                    auto _scne = Scene::create();
                    _scne->addChild(HomeLayer::create());
                    Director::getInstance()->replaceScene(TransitionPageTurn::create(1.2,_scne,false));
                };
                if(IAPManager::getInstance()->isShowAds()){
                    LQ_adLoadingLayer::loadingDoneCallback = _endcallBack;
                    LQ_adLoadingLayer::showLoading<LQ_adLoadingLayer>(false);
                } else
                    _endcallBack();
                
            }
        };
        Director::getInstance()->getRunningScene()->addChild(_dialog);
    });
    
    BaseStepLayer::setKeyBack([](){
        auto _dialog = MyDialog::create("Do you want to start over with a new game?", MyDialog::twoBtnPath);
        _dialog->dialogBtnClick =[](MyDialog * dialog,int index){
            if(index == 0) {
                auto _endcallBack = [](){
                    Director::getInstance()->popToRootScene();
                    LQ_adLoadingLayer::loadingDoneCallback = nullptr;
                    auto _scne = Scene::create();
                    _scne->addChild(HomeLayer::create());
                    Director::getInstance()->replaceScene(TransitionPageTurn::create(1.2,_scne,false));
                };
                if(IAPManager::getInstance()->isShowAds()){
                    LQ_adLoadingLayer::loadingDoneCallback = _endcallBack;
                    LQ_adLoadingLayer::showLoading<LQ_adLoadingLayer>(false);
                } else
                    _endcallBack();
                
            }
        };
        
        Director::getInstance()->getRunningScene()->addChild(_dialog);
        return true;
    });
}

void LoadingLayer::onEnterTransitionDidFinish() {
    BaseStepLayer::onEnterTransitionDidFinish();

}